import { Component, OnInit } from '@angular/core';
import { Response } from '@angular/http';

import {GoodsDetailService} from '../service/goods-detail.service';
import {CartService} from '../service/cart.service';

@Component({
  selector: 'app-goods-detail',
  templateUrl: './goods-detail.component.html',
  styleUrls: ['./goods-detail.component.less'],
  providers: [GoodsDetailService]
})
export class GoodsDetailComponent implements OnInit {
  images;
  product;
  quantity = 1;
  address = {
    province: '',
    city: '',
    county: '',
    selectedPro: false,
    selectedCity: false,
    selectedCounty: false,
    selectedAll: false
  };
  constructor(private goodsDetailService: GoodsDetailService, private cartService: CartService) { }

  ngOnInit() {
    const detailAddress = localStorage.getItem('address');
    if (detailAddress) {
      const  saddress = JSON.parse(detailAddress);
      console.log(saddress);
      this.detailAddress(saddress);
    }
    this.goodsDetailService.getDetailImg().subscribe(
      (goodsDetail) => {
        console.log(goodsDetail.data.product);
        this.product = goodsDetail.data.product;
        this.product['quantity'] = this.quantity;
        console.log(goodsDetail.data.product);
        this.images = goodsDetail.data.product.images;
      }
    );
  }
  detailAddress(address) {
    this.address.province = address.selectedProvince;
    this.address.city = address.selectedCity;
    this.address.county = address.selectedCounty;
    this.address.selectedAll = address.allWasSelected;
  }
  plusQuantity() {
    this.quantity += 1;
    this.product['quantity'] = this.quantity;
  }
  minusQuantity() {
    if (this.quantity > 1) {
      console.log('a');
      this.quantity -= 1;
      this.product['quantity'] = this.quantity;
    }else {
      console.log('b');
      this.quantity = 1;
      this.product['quantity'] = this.quantity;
    }
  }
  changeQuantity(number: number) {
    this.product['quantity'] = number;
  }
  onAddToCart(product) {
    this.cartService.onAddProduct(product);
  }
}
